public class Leet200 {

	public static void main(String[] args) {
		

	}
	public int numIslands(char[][] grid) {
		int num = 0;	
		for(int i = 0;i<grid.length;i++)
			for(int j = 0;j<grid[0].length;j++)
				num+=dfs(grid,i,j)?1:0;
		return num;
    }
	boolean dfs(char[][] grid,int m ,int n){
		if(grid[m][n]!='1')
			return false;
		grid[m][n] = '3';
		if(m>0)
			dfs(grid,m-1,n);
		if(m<grid.length-1)
			dfs(grid,m+1,n);
		if(n>0)
			dfs(grid,m,n-1);
		if(n<grid[0].length-1)
			dfs(grid,m,n+1);
		return true;
	}

}
